package demo;

public class erfenchazhao {
    public static void main(String[] args) {
        int[] arr = {1,3,5,7,9,11};
        int key = 4;
        int position = commonbinarySearch(arr, key);
        if(position == -1){
            System.out.println("查找的是"+key+",序列中没有该数！");
        }else{
            System.out.println("查找的是"+key+",找到位置为："+position);
        }
    }
    public static int  commonbinarySearch(int arr[],int key){
        int low=0;
        int height =arr.length-1;
        int middle=0;
        if(key<arr[low]||key>arr[height]||low>height){
            return -1;
        }
        while(low<=height){
            middle=(low+height)/2;
            System.out.println("middle = " + middle);
            if(key>arr[middle]){
                low=middle+1;
            }else if(key<arr[middle]){
                height=middle-1;
            }else{
                return middle;
            }
        }
        return -1;
    }

}
